﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using Business;
using Common;
using Dto;

namespace YaoYunDong.Controllers
{
    public class GroupController : ChannelBaseController
    {
        private readonly string _groupNarrowPath = ConfigurationManager.AppSettings["GroupNarrowPath"];
        private readonly string _groupOriginalPath = ConfigurationManager.AppSettings["GroupOriginalPath"];
        private readonly ChannelBusiness channelBusiness = new ChannelBusiness();
        private readonly SportsGroupBusiness groupBusiness = new SportsGroupBusiness();
        private readonly GroupTopicBusiness topicBusiness = new GroupTopicBusiness();
        private readonly MovingBusiness movingBusiness = new MovingBusiness();

        public ActionResult Index(string code)
        {
            ViewData["GroupCode"] = code;
            var groupDto = groupBusiness.GetGroupByCode(code);
            ViewData["GroupMovings"] = movingBusiness.GetGroupUserMovings(groupDto.GroupId);
            return View("~/Views/Group/" + groupDto.GroupTemplateId + "/Index.aspx", groupDto);
        }

        public ActionResult UploadPhoto(string code)
        {
            var groupDto = groupBusiness.GetGroupByCode(code);
            ViewData["Code"] = code;
            return View("~/Views/Group/" + groupDto.GroupTemplateId + "/UploadPhoto.aspx", groupDto);
        }

        public ActionResult Topics(string code, int? currentPageNo)
        {
            var userDto = Session["User"] as UsersDto ?? new UsersDto();
            var groupDto = groupBusiness.GetGroupByCode(code);
            ViewData["Code"] = code;
            ViewData["GroupId"] = groupDto.GroupId;
            ViewData["UserId"] = userDto.UserId;
            int totalCount;
            var dtos = topicBusiness.GetGroupTopics(currentPageNo ?? 1, 20, groupDto.GroupId, out totalCount);
            var page = new PagingDto(totalCount, currentPageNo ?? 1, 20, dtos ?? new List<GroupTopicDto>());
            return View("~/Views/Group/" + groupDto.GroupTemplateId + "/Topics.aspx", page);
        }

        public ActionResult CreateGroup(string code)
        {
            var channelDto = SiteChannelDictionary[code];
            ViewData["Id"] = code;
            ViewData["SiteChannels"] = channelBusiness.GetShowList();
            ViewData["ChannelDto"] = channelDto;
            return View();
        }

        public ActionResult Menu(int templateId, string code, string current)
        {
            ViewData["Code"] = code;
            ViewData["Current"] = current;
            return View("~/Views/Group/" + templateId + "/Menu.ascx");
        }

        public ActionResult Top(string code)
        {
            var groupDto = groupBusiness.GetGroupByCode(code);
            return View("~/Views/Group/" + groupDto.GroupTemplateId + "/Top.ascx", groupDto);
        }

        //public ActionResult CreateActivity(string code)
        //{
        //    var groupDto = groupBusiness.GetGroupByCode(code);
        //    return View("~/Views/Group/" + groupDto.GroupTemplateId + "/CreateActivity.aspx");
        //}

        public ActionResult CreateActivity(string code)
        {
            var groupDto = groupBusiness.GetGroupByCode(code);
            ViewData["GroupId"] = groupDto.GroupId;
            return Session["ActivityDto"] != null
                       ? View("~/Views/Group/" + groupDto.GroupTemplateId + "/CreateActivity-Step1.aspx")
                       : View("~/Views/Group/" + groupDto.GroupTemplateId + "/CreateActivity-Step2.aspx");
        }

        public ActionResult ActivityList()
        {
            return View();
        }

        #region 创建战队
        /// <summary>
        /// 创建战队
        /// </summary>
        /// <param name="name"></param>
        /// <param name="summary"></param>
        /// <param name="validate"></param>
        /// <param name="selectChannel"></param>
        /// <param name="code"></param>
        /// <param name="templateId"></param>
        /// <param name="imgName"></param>
        /// <returns></returns>
        public ActionResult Create(string name, string summary, string validate, string[] selectChannel, string code,
                                   int templateId, string imgName)
        {
            var annexUploadUtil = new AnnexUploadUtil();
            AnnexDto annexDto = null;
            if (!string.IsNullOrEmpty(imgName))
            {
                annexDto = annexUploadUtil.ImageUpload("/TempImage/" + imgName, imgName, 160, 160, _groupNarrowPath,
                                                       _groupOriginalPath, true);
            }
            var dto = new SportsGroupDto
                          {
                              GroupChannel = "," + string.Join(",", selectChannel) + ",",
                              GroupCreator = UserInfo.UserId,
                              GroupName = name,
                              GroupTime = DateTime.Now,
                              GroupSummary = summary,
                              GroupCode = code,
                              GroupTemplateId = templateId
                          };
            groupBusiness.SaveGroup(dto, annexDto);
            return View();
        }
        #endregion
    }
}